package com.roc.web.action.terminal;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.roc.bean.TermBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Esmidinfo;
import com.roc.pojo.EsmidinfoTmp;
import com.roc.pojo.Estidinfo;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.pojo.TblMchtInf;
import com.roc.pojo.TblTermInf;
import com.roc.pojo.TermInfo;
import com.roc.pojo.YjPosInfo;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.ITerminalService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigMerinfoUtil;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.TerminalBrand;
import com.roc.util.constant.TerminalFlag;
import com.roc.util.constant.TerminalName;
import com.roc.util.constant.TerminalType;

/**
 * 
 * @author 陈锴
 * 
 * 终端序列号添加 
 * 
 * 2014-08-18
 *
 */

@Component
@Scope("prototype")
@Namespace("/terminalSn")
public class TerminalSnQueryAction extends PaginationBaseAction{
	
	private Logger logger=LogManager.getLogger(TerminalSnQueryAction.class);
	
	private TerminalSnQuerySearchCondition searchCondition;
	
	private Estidinfo estid;
	private TblMchtInf tblMchtInf;
	private String msg;
	
	private List<TermBean> termBeanList;
	
	private Esmidinfo esmidinfo;
	
	private List<OrgBean> orgList=new ArrayList<OrgBean>();
	
	private YjPosInfo yjPos;
	
	private TermInfo termInfo;
	
	private TblTermInf termInfos;
	
	private String mid;
	
	private String tid;
	
	private String remark;//备注
	
	private int deal;
	@Autowired
	IorgService orgService;
	
	@Autowired
	IPageService pagenitionService;
	
	@Autowired
	ITerminalService terminalService;
	
	@Autowired
	IMerchantServices merchantService;
	
	@Autowired 
	IMerchantServices merchantServices;
	
	@Action(value = "terminalSnJsp", results = { @Result(name = "success", location = "/pages/terminal/terminal_sn.jsp")
             })
	public String terminalSnJspAction() throws Exception{
		try {
			tblMchtInf=merchantServices.queryMerchat(mid);
			termInfos=terminalService.queryTermInfopart(tid,mid);;
			yjPos=terminalService.queryYjPosInfo(termInfos.getDeviceCd(),termInfos.getTmnlBrand());
			if(yjPos!=null){
				yjPos.setPosName(ConfigMerinfoUtil.getPosMODEL(yjPos.getPosName(), yjPos.getPosBrand()));
				yjPos.setPosBrand(ConfigMerinfoUtil.getPosBRAND(yjPos.getPosBrand()));
				yjPos.setPosType(ConfigMerinfoUtil.getPosType(yjPos.getPosType()));
			}			
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		
		return SUCCESS;
	}
	
	@Action(value = "terminalClear", results = { @Result(name = "success", location = "/pages/terminal/terminal_sn.jsp")
    })
	public String terminalClearAction() throws Exception{
		Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		
		try {
			terminalService.clearTermInfo(logingMerchant.getAccountNo(), tid, mid, yjPos.getSerialNum(), remark);
			
			msg="终端操作成功";
			getRequest().setAttribute("remark", remark);
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		return terminalSnJspAction();
	}
	
	@Action(value = "terminalSnList", results = { @Result(name = "success", location = "/pages/terminal/terminal_sn.jsp")
           })
	//查询终端流水号
	public String terminalSnListAction() throws Exception{
	
		if(searchCondition==null){
			searchCondition=(TerminalSnQuerySearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new TerminalSnQuerySearchCondition();
			}
		}
		try {
			
			org();
			esmidinfo();
			
			int rows=pagenitionService.count(searchCondition);
			searchRsList=pagenitionService.findAll(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);

			orgTheListSpecially(searchRsList);
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		} catch (ServiceException e) {
			
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			
		}
		
		return SUCCESS;
	}
	
	@Action(value = "bindingTerminalSn", results = { @Result(name = "success",location = "/pages/terminal/terminal_sn.jsp")
    })
	//查询终端流水号
	public String bindingTerminalSnAction() throws Exception{
		Operators logingMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		org();
		esmidinfo();
		try {
			terminalService.bindingTerminalSn(yjPos, logingMerchant.getAccountNo());
			
			msg="绑定终端流水号成功";
		} catch (ServiceException e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
		}
		
		return SUCCESS;
	}
	
	//查询机构
	public void org() throws Exception{
	
			Operators logingOperators=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			orgList=orgService.listOrgs(logingOperators);

			getRequest().setAttribute("orgList", orgList);
	}
	
	//查询商户基本信息
	public void esmidinfo() throws Exception{
		esmidinfo=merchantService.queryEsmidinfoTmp(mid);
		
	}
	
	protected void orgTheListSpecially(List<Object> estidList) {
    	List<Object> tempList=new ArrayList<Object>();
		for(Object obj:estidList){
			YjPosInfo temp=(YjPosInfo)obj;
			for(OrgBean bean:orgList){
				if(temp.getOrgCode().equals(bean.getOrg_code())){
					temp.setOrgCode(bean.getOrg_name());
				}
			}
			
			tempList.add(temp);
		}
		searchRsList=tempList;
	}
	
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSession()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (TerminalApplySearchCondition)getSession().getAttribute(SEARCH_SESSION_BEAN_TERMINAL_SN_QUERY);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#getSearchConditionFromSubClass()
	 */
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchConditionToSubClass(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	/* (non-Javadoc)
	 * @see com.roc.page.PaginationBaseAction#setSearchToConditionSession(com.roc.page.BaseSearchCondition)
	 */
	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_TERMINAL_SN_QUERY, searchCondition);
	}

	public TerminalSnQuerySearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(TerminalSnQuerySearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<TermBean> getTermBeanList() {
		return termBeanList;
	}

	public void setTermBeanList(List<TermBean> termBeanList) {
		this.termBeanList = termBeanList;
	}

	public Estidinfo getEstid() {
		return estid;
	}

	public void setEstid(Estidinfo estid) {
		this.estid = estid;
	}

	public List<OrgBean> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<OrgBean> orgList) {
		this.orgList = orgList;
	}

	public YjPosInfo getYjPos() {
		return yjPos;
	}

	public void setYjPos(YjPosInfo yjPos) {
		this.yjPos = yjPos;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public void setEsmidinfo(Esmidinfo esmidinfo) {
		this.esmidinfo = esmidinfo;
	}

	public Esmidinfo getEsmidinfo() {
		return esmidinfo;
	}

	public TermInfo getTermInfo() {
		return termInfo;
	}

	public void setTermInfo(TermInfo termInfo) {
		this.termInfo = termInfo;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public TblMchtInf getTblMchtInf() {
		return tblMchtInf;
	}

	public void setTblMchtInf(TblMchtInf tblMchtInf) {
		this.tblMchtInf = tblMchtInf;
	}

	public TblTermInf getTermInfos() {
		return termInfos;
	}

	public void setTermInfos(TblTermInf termInfos) {
		this.termInfos = termInfos;
	}

	public int getDeal() {
		return deal;
	}

	public void setDeal(int deal) {
		this.deal = deal;
	}
	
}
